#coding=utf8
#_author_: chanceyang

import copy
import json
from requests import Session
from utils.environments_json import urlHead
from utils.common_dcc import response_time

# /topicapi/tmsExam/front/saveExam
def saveExamRequest(uri,data:str ,mockId ,paperId ,session,examType=3,isRestart=False):
    url=urlHead+uri
    data:dict= json.loads(data)
    data["mockId"] = mockId
    data["paperId"] = paperId
    data["examType"]=examType
    if examType=="null":
        data["examType"] = None
    elif examType=="none":
        data.pop("examType")
    if type(data["isRestart"])!=type(False):
        data["isRestart"]=isRestart
        if data["isRestart"]=="null":
            data["isRestart"]=None
        elif data["isRestart"]=="none":
            data.pop("isRestart")
    data = json.dumps(data, ensure_ascii=False).encode("utf-8")
    res = session.post(url=url, data=data)
    resDic = json.loads(res.text)
    response_time(uri,res)
    return resDic ,res


# /topicapi/tmsMock/front/frontFetchPaperInfoByMockIdAndPaperId
def fetchTopicsRequest(uri,data:str,examId,session):
    url = urlHead + uri
    data = json.loads(data)
    data["examId"] = examId
    data = json.dumps(data, ensure_ascii=False).encode("utf-8")
    res = session.post(url=url, data=data)
    resDic = json.loads(res.text)
    response_time(uri, res)
    return resDic, res


# /topicapi/tmsExam/front/cacheMockAnswer
def cacheMockAnswerRequest(uri,data:str,res:dict,examId,mockId,paperId,session,answers=None):
    return handPagerRequest(uri,data,res,examId,mockId,paperId,session,answers)


# /topicapi/tmsExam/front/handPaper
def handPagerRequest(uri,data:str,res:dict,examId,mockId,paperId,session,answers=None):
    url = urlHead + uri
    # 第一道题的所有小问，在输入框中输入特殊字符等
    answer_1="接口自动化测试\n\\(  \\gamma \\mu \\phi \\varphi \\pm \\dagger \\ddagger \\infty \\ne \\geq \\geq \\leq \\simeq \\begin{pmatrix}a&b\\\\c&d\\\\ \\end{pmatrix} \\coprod_{a}^{b} x_{a}^{b} x^{b} \\prod_{a}^{b} \\lim_{a \\rightarrow b}{x} \\leftrightarrow \\Leftarrow \\Rightarrow \\)\n特殊字符~~`!@#$%^&*()_+=__--{}[]\":;',.<>,.?/dfasdgasdgsfasdf"

    # 第二道主观题，第一个小问，表格作答，额外输入框作答
    answer_2=r'{"table":[[{"rowIdx":"1","colIdx":"0","value":"花木成畦手自栽花木成畦手自栽"},{"rowIdx":"1","colIdx":"1","value":"茜a"},{"rowIdx":"1","colIdx":"2","value":"茜"},{"rowIdx":"1","colIdx":"3","value":"茜"},{"rowIdx":"2","colIdx":"0","value":"茜"},{"rowIdx":"2","colIdx":"1","value":"革"},{"rowIdx":"2","colIdx":"2","value":"茜"},{"rowIdx":"2","colIdx":"3","value":"革"},{"rowIdx":"3","colIdx":"0","value":"茜"},{"rowIdx":"3","colIdx":"1","value":"茜"},{"rowIdx":"3","colIdx":"2","value":"茜"},{"rowIdx":"3","colIdx":"3","value":"花木成畦手自栽 "}],[{"rowIdx":"0","colIdx":"0","value":"\\(  \\Upsilon \\varphi \\delta \\dagger \\bot \\ne \\succ \\left( x \\right) \\tilde{x} \\Leftarrow \\leftrightarrow \\)"},{"rowIdx":"0","colIdx":"1","value":"\\(  \\supseteq \\gg \\in \\tilde{x} \\bar{x} x_{a}^{b} \\)"},{"rowIdx":"0","colIdx":"2","value":" "},{"rowIdx":"0","colIdx":"3","value":" "},{"rowIdx":"0","colIdx":"4","value":" "},{"rowIdx":"0","colIdx":"5","value":" "},{"rowIdx":"1","colIdx":"0","value":" "},{"rowIdx":"1","colIdx":"1","value":" "},{"rowIdx":"1","colIdx":"2","value":" "},{"rowIdx":"1","colIdx":"3","value":" "},{"rowIdx":"1","colIdx":"4","value":" "},{"rowIdx":"1","colIdx":"5","value":" "},{"rowIdx":"2","colIdx":"0","value":" "},{"rowIdx":"2","colIdx":"1","value":" "},{"rowIdx":"2","colIdx":"2","value":" "},{"rowIdx":"2","colIdx":"3","value":" "},{"rowIdx":"2","colIdx":"4","value":" "},{"rowIdx":"2","colIdx":"5","value":" "},{"rowIdx":"3","colIdx":"0","value":" "},{"rowIdx":"3","colIdx":"1","value":" "},{"rowIdx":"3","colIdx":"2","value":" "},{"rowIdx":"3","colIdx":"3","value":" "},{"rowIdx":"3","colIdx":"4","value":" "},{"rowIdx":"3","colIdx":"5","value":" "}]],"input":"输入框中作答"}'

    # 第二道主观题，第二个小问及之后的普通作答
    answer_3="接口测试答案输入"


    if answers==None:
        answers={"otopics":{"danxuan":"C","duoxuan":"A,C"},"stopics":[[answer_1],[answer_2,answer_3]]}

    data = json.loads(data)
    otopicTmp = data["otopicList"][0]
    stopicTmp = data["stopicList"][0]
    res_otopicList = res["data"]["otopics"]
    res_stopicList = res["data"]["stopics"]
    otopicList = []
    stopicList = []
    # 生成客观题作答
    if res_otopicList != None and res_otopicList != []:
        for otopic in res_otopicList:
            otopicTmp1 = copy.deepcopy(otopicTmp)
            if otopic["optionType"] == 1:
                otopicTmp1["topicId"] = otopic["topicId"]
                otopicTmp1["answer"] = answers["otopics"]["danxuan"]
                otopicTmp1["costTime"] = 10
                otopicTmp1["isAnswer"] = 1
                otopicTmp1["topicNum"] = otopic["number"]
            if otopic["optionType"] == 2:
                otopicTmp1["topicId"] = otopic["topicId"]
                otopicTmp1["answer"] = answers["otopics"]["duoxuan"]
                otopicTmp1["costTime"] = 10
                otopicTmp1["isAnswer"] = 1
                otopicTmp1["topicNum"] = otopic["number"]
            otopicList.append(otopicTmp1)

    # 生成主观题作答
    if res_stopicList != None and res_stopicList != []:
        stopic1 = res_stopicList[0]
        stopicTmp1 = copy.deepcopy(stopicTmp)
        stopicTmp1["isAnswer"] = 1
        stopicTmp1["stopicId"] = stopic1["topicId"]
        stopicTmp1["topicNum"] = stopic1["number"]
        stopicTmp1["totalTime"] = 25
        questionList = []
        questionTmp = stopicTmp1["answers"][0]
        for question in stopic1["squestions"]:
            questionTmp1 = copy.deepcopy(questionTmp)
            questionTmp1["answer"] = answers["stopics"][0][0]
            questionTmp1["costTime"] = 11
            questionTmp1["squestionId"] = question["id"]
            questionList.append(questionTmp1)

        stopicTmp1["answers"] = questionList
        stopicList.append(stopicTmp1)

        stopic2 = res_stopicList[1]
        stopicTmp1 = copy.deepcopy(stopicTmp)
        stopicTmp1["isAnswer"] = 1
        stopicTmp1["stopicId"] = stopic2["topicId"]
        stopicTmp1["topicNum"] = stopic2["number"]
        stopicTmp1["totalTime"] = 25
        questionList = []
        questionTmp = stopicTmp1["answers"][0]
        i = 0
        for question in stopic2["squestions"]:
            if i == 0:
                questionTmp1 = copy.deepcopy(questionTmp)
                questionTmp1["answer"] = answers["stopics"][1][0]
                questionTmp1["costTime"] = 11
                questionTmp1["squestionId"] = question["id"]
                questionList.append(questionTmp1)
                i += 1
            else:
                questionTmp1 = copy.deepcopy(questionTmp)
                questionTmp1["answer"] = answers["stopics"][1][1]
                questionTmp1["costTime"] = 11
                questionTmp1["squestionId"] = question["id"]
                questionList.append(questionTmp1)
        stopicTmp1["answers"] = questionList
        stopicList.append(stopicTmp1)

    reqData = data
    reqData["chapterId"] = 0
    reqData["examId"] = examId
    reqData["mockId"] = mockId
    reqData["paperId"] = paperId
    reqData["totalTime"] = 200
    reqData["otopicList"] = otopicList
    reqData["stopicList"] = stopicList
    reqData = json.dumps(reqData, ensure_ascii=False).encode("utf-8")
    res = session.post(url=url, data=reqData)
    resDic = json.loads(res.text)
    resDic["otopicsAnswersList"]=otopicList
    response_time(uri,res)
    return resDic,res


# /topicapi/tmsExam/front/fetchExamResultSimple
def fetchExamResultSimpleRequest(uri,examId,data:str,session):
    url = urlHead + uri
    data = json.loads(data)
    data["examId"] = examId
    res = session.get(url=url, params=data)
    resDic = json.loads(res.text)
    response_time(uri,res)
    return resDic,res


# /topicapi/tmsExam/front/fetchSubjectInfoSingle
def fetchSubjectInfoSingleRequest(uri,stopicId,recordId,data:str,session):
    url = urlHead + uri
    data = json.loads(data)
    data["stopicId"] = stopicId
    data["recordId"] = recordId
    res=session.get(url=url, params=data)
    resDic=json.loads(res.text)
    response_time(uri, res)
    return resDic,res

# /topicapi/tmsExam/front/frontListExamRecord
def frontListExamRecord(uri,data:str,session):
    url = urlHead + uri
    data=json.dumps(data, ensure_ascii=False).encode("utf-8")
    res=session.post(url=url,data=data)
    resDic=json.loads(res.text)
    response_time(uri,res)
    return resDic,res





#客观题做题交卷
def handOtopicsInMock(saveExamRequestUri,saveExamRequestData:str,fetchTopicsRequestUri,fetchTopicsRequestData:str,handPagerRequestUri,handPagerRequestData:str,mockId,paperId,session,dicAnswer):
    # 第一道题的所有小问，在输入框中输入特殊字符等
    answer_1 = ""
    # 第二道主观题，第一个小问，表格作答，额外输入框作答
    answer_2 = ""
    # 第二道主观题，第二个小问及之后的普通作答
    answer_3 = ""
    mydic={}
    resDic, res = saveExamRequest(saveExamRequestUri, saveExamRequestData,mockId, paperId,session)

    examId = resDic["data"]["examId"]
    mydic["examId"]=examId
    resDic, res = fetchTopicsRequest(fetchTopicsRequestUri, fetchTopicsRequestData,examId,session)
    mydic["resTopics"]=resDic

    answers = {"otopics": dicAnswer, "stopics": [[answer_1], [answer_2, answer_3]]}

    handPagerRequest(handPagerRequestUri, handPagerRequestData,mydic["resTopics"], mydic["examId"], mockId,paperId,session, answers)
    return mydic




def insertUserNoteRequest(uri,userId,topicId,squestionId,note,data:str,session:Session):
    url = urlHead + uri
    data=json.loads(data)
    data["userId"]=userId
    data["topicId"]=topicId
    data["squestionId"]=squestionId
    data["note"]=note
    data=json.dumps(data,ensure_ascii=False).encode("utf-8")
    res=session.post(url=url,data=data)
    resDic=json.loads(res.text)
    return resDic,res

if __name__=="__main__":
   str="""{"bool":false}"""
   str_d=json.loads(str)
   str_d["bool"]=None
   s=json.dumps(str_d)
   print(s)